package com.example.demo;

import com.example.demo.annotation.AnonymousAccess;
import com.example.demo.utils.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.InetAddress;
import java.net.UnknownHostException;
@EnableAsync
@RestController
@EnableTransactionManagement
//springboot注解
@SpringBootApplication
//日志log4j
@Slf4j
public class SpringBootSystemApplication {

    public static void main(String[] args) throws UnknownHostException {
        ConfigurableApplicationContext application = SpringApplication.run(SpringBootSystemApplication.class, args);
        Environment env=application.getEnvironment();
        //        应用
        log.info("\n-------------------------------------------\n\t" +
                "Application '{}' is running!Access URLs:\n\t" +
                "Local:\t\thttp://localhost:{}{}\n\t" +
                "External:\thttp://{}:{}{}\n\t" +
                "Doc:\thttp://{}:{}{}doc.html\n" +
                "-------------------------------------------------",
                env.getProperty("server.servlet.context-path"),
                env.getProperty("server.port"),
                env.getProperty("server.servlet.context-path"),
                InetAddress.getLocalHost().getHostAddress(),
                env.getProperty("server.port"),
                env.getProperty("server.servlet.context-path"),
                InetAddress.getLocalHost().getHostAddress(),
                env.getProperty("server.port"),
                env.getProperty("server.servlet.context-path"));
    }

    @Bean
    public SpringContextHolder springContextHolder(){
        return new SpringContextHolder();
    }
    @Bean
    public ServletWebServerFactory webServerFactory(){
        TomcatServletWebServerFactory fa = new TomcatServletWebServerFactory();
        fa.addConnectorCustomizers(connector -> connector.setProperty("relaxedQueryChars","[]{}"));
        return fa;
    }
    @GetMapping("/")
    @AnonymousAccess
    public String index(){
        return "Backend service started successfully";
    }
}
